/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-05-13     Administrator       the first version
 */
#include "motor.h"

struct rt_device_pwm *pwm_dev12; /* PWM设备句柄  左前和左后*/
struct rt_device_pwm *pwm_dev5;  /* PWM设备句柄 右前和右后*/

int TIM_PWM_init(void)
{
    /* 查找设备 */
    pwm_dev5 = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME5);
    pwm_dev12 = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME12);
    if(pwm_dev5 == RT_NULL)
    {
        rt_kprintf("device5 : %s find failed!\n", PWM_DEV_NAME5);
        return RT_ERROR;
    }
    if(pwm_dev12 == RT_NULL)
    {
        rt_kprintf("device12 : %s find failed!\n", PWM_DEV_NAME12);
        return RT_ERROR;
    }
    for(int channal = 3; channal < 5; channal++)
    {
        /* 设置PWM周期和脉冲宽度默认值 */
        rt_pwm_set(pwm_dev5, channal, PERIOD, 0);//初始化先不动
        /* 使能设备 */
        rt_pwm_enable(pwm_dev5, channal);
    }
    for(int channal = 1; channal < 3; channal++)
    {
        /* 设置PWM周期和脉冲宽度默认值 */
        rt_pwm_set(pwm_dev12, channal, PERIOD, 0);//初始化先不动
        /* 使能设备 */
        rt_pwm_enable(pwm_dev12, channal);
    }

    rt_kprintf("PWM enable succeed\n");
    return RT_EOK;
}

//初始化电机接口
void Enable_Pin(void)
{
    /*配置引脚模式*/
    rt_pin_mode(Left_ahead_AIN1, PIN_MODE_OUTPUT);
    rt_pin_mode(Left_ahead_AIN2, PIN_MODE_OUTPUT);

    rt_pin_mode(Left_back_BIN1, PIN_MODE_OUTPUT);
    rt_pin_mode(Left_back_BIN2, PIN_MODE_OUTPUT);

    rt_pin_mode(right_ahead_AIN1, PIN_MODE_OUTPUT);
    rt_pin_mode(right_ahead_AIN2, PIN_MODE_OUTPUT);

    rt_pin_mode(right_back_BIN1, PIN_MODE_OUTPUT);
    rt_pin_mode(right_back_BIN2, PIN_MODE_OUTPUT);

    /* 设置低电平 */
    rt_pin_write(Left_ahead_AIN1, PIN_LOW);
    /* 设置低电平 */
    rt_pin_write(Left_ahead_AIN2, PIN_LOW);
    /* 设置低电平 */
    rt_pin_write(Left_back_BIN1, PIN_LOW);
    /* 设置低电平 */
    rt_pin_write(Left_back_BIN2, PIN_LOW);
    /* 设置低电平 */
    rt_pin_write(right_ahead_AIN1, PIN_LOW);
    /* 设置低电平 */
    rt_pin_write(right_ahead_AIN2, PIN_LOW);
    /* 设置低电平 */
    rt_pin_write(right_back_BIN1, PIN_LOW);
    /* 设置低电平 */
    rt_pin_write(right_back_BIN2, PIN_LOW);

    rt_kprintf("PIN enable succeed\r\n");
}



